package applet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import lmf.*;
import musicbrainz.*;

import processing.core.PApplet;
import processing.core.PImage;

class ArtistDragMenu extends GuiElement
{
	public PImage icon1,icon2;
	public  String label;
	public  int fontSize;
	public int dragDownSize = 10;
	public boolean color = false;
	public boolean fixedSize = true;
	public VerticalScrollBar selectBar;
	public Button upButton, downButton;
	public boolean onUpButton, onDownButton;
	
	public ArrayList<ArtistButton> items;
	
	private boolean justPressed = false;
	public boolean onItem = false;
	public int onItemIndex = -1;
	
	public ArtistDragMenu(ArrayList<ArtistButton> subMenu)
	{
		fixedSize = true;
		height = 20;
		fontSize = 16;
		items = subMenu;
		
		upButton = new Button();
		downButton = new Button();
		upButton.fixedSize = false;
		downButton.fixedSize = false;
		//upButton.width = 20;
		//downButton.width = 20;
		//upButton.setLabel("");
		//downButton.setLabel("");
		upButton.setIcon(GLOBAL.baseDir + "uparrow.png");
		downButton.setIcon(GLOBAL.baseDir + "downarrow.png");
		upButton.height = 18;
		downButton.height = 18;
	}
	
	public ArtistDragMenu(int cx, int cy, ArrayList<ArtistButton> subMenu)
	{
		fixedSize = true;
		height = 20;
		items = subMenu;
		fontSize = 16;
		x = cx;
		y = cy;
		
		upButton = new Button();
		downButton = new Button();
		upButton.fixedSize = false;
		downButton.fixedSize = false;
		//upButton.width = 20;
		//downButton.width = 20;
		//upButton.setLabel("");
		//downButton.setLabel("");
		upButton.setIcon(GLOBAL.baseDir + "uparrow.png");
		downButton.setIcon(GLOBAL.baseDir + "downarrow.png");
		upButton.height = 18;
		downButton.height = 18;
	}

	public void setIcon(String name1,String name2)
	{
		if(name1 != null && name2!=null)
		{
			icon1 = GLOBAL.processing.loadImage(name1); //down
			icon2 = GLOBAL.processing.loadImage(name2); //up
			
		}
	}

	public void setLabel(String name)
	{
		label = name;
		GLOBAL.processing.textFont(GLOBAL.tFont, fontSize);
		
		if(fixedSize)
		{
			width = GLOBAL.defaultMenuWidth - 18;
			if(GLOBAL.processing.textWidth(name)>width)
				fontSize = (int) (2 * width / name.length()) -8;
			if(fontSize==0)
				fontSize = 1;
		}
		else
			width = (int)(GLOBAL.processing.textWidth(name) + 10);
		
		if(icon1 != null || icon2!=null){width += 18;}
		
		if(GLOBAL.getItemsSize()> dragDownSize)
		{
			selectBar = new VerticalScrollBar();
			selectBar.x = x + width + 37;
			selectBar.y = y + height + 20;	
			selectBar.width = 14;
			selectBar.height = items.get(0).height * dragDownSize - 2*20;	
			selectBar.size = (float)1 / 10;
			
			upButton.x = x + width + 35;
			upButton.y = y + height;
			downButton.x = x + width + 35;
			downButton.y = selectBar.y + selectBar.height;
		}
	}

	public void draw()
	{
		//int cx = x;
		//int cy = y;
		
		//GLOBAL.processing.fill(GLOBAL.colorBackground);
		//GLOBAL.processing.rectMode(PApplet.CORNER);
		//GLOBAL.processing.rect(x, y, width, height);
		//GLOBAL.gu.drawBox(x, y, width, height, 0, GLOBAL.processing.color(150, 150, 200), 255);
		//GLOBAL.gu.drawBox(x, y, width, height, 1, GLOBAL.processing.color(150, 150, 200), 150);

		//if(mouseOver()) 
		//{
		//	super.draw();
		//}
		//if(active)
		//{
		//	cx += 2;
		//	cy += 2;
		//}	
		//if(label != null)
		//{
		//	GLOBAL.processing.textFont(GLOBAL.tFont, fontSize);
		//	GLOBAL.processing.textAlign(GLOBAL.processing.CENTER);
		//	GLOBAL.processing.strokeWeight(1);
		//	GLOBAL.processing.fill(GLOBAL.colorButtonLabel);
		//	//GLOBAL.processing.text(label, cx + 5, cy + 20);
		//	GLOBAL.processing.text(label, cx + (width-18)/2, cy + height/2+5);
		//}

		//if(active)
		//{
		//GLOBAL.processing.fill(GLOBAL.colorButtonActive);
		//GLOBAL.processing.rect(x, y, width, height);
		//draw the menu of sub-categories
		if(onUpButton && upButton.mouseOver())
		{
			selectBar.value -= 0.5/(float)GLOBAL.getItemsSize();
			if(selectBar.value<=0)
				selectBar.value = 0;
		}
		else if(onDownButton && downButton.mouseOver())
		{
			selectBar.value += 0.5/(float)GLOBAL.getItemsSize();
			if(selectBar.value>=0.9)
				selectBar.value = (float)0.9;
		}

		showSubs();
		//}
		//if(GLOBAL.mouseDragged)
		//{
		//	mouseDragged();
		//}
	}

	public void setFontSize(int size) {
		fontSize = size;
	}

	
	public void showSubs()
	{
		float val = 0;
		if(GLOBAL.getItemsSize()>dragDownSize)
		{
			selectBar.draw();
			upButton.draw();
			downButton.draw();
			// Find from what ArtistButton we have to print, based on the value of the scrollbar in this moment
			val = PApplet.map(selectBar.value,0, 1- selectBar.size,0,GLOBAL.getItemsSize() - dragDownSize +1);
		}
		else
			val = 0;
		// Draw all the visible series ArtistButtons
		for(int j = 0; j<GLOBAL.getItemsSize(); j++) {
			if( j >= (int)val && j < (int)val + dragDownSize && j < GLOBAL.getItemsSize()) 
			{
				items.get(j).x = x;
				items.get(j).y = y +(j-(int)val+1)*height;
				//items.get(j).active = true;
				items.get(j).width = width+30;
				items.get(j).height = height - 2;
				//items.get(j).setLabel(GLOBAL.categoryNames[j]);
				//items.get(j).setIcon(GLOBAL.baseDir + ""+GLOBAL.categoryNames[j].toLowerCase() +".png");
				items.get(j).draw();
			}
			else {
				items.get(j).x = 2000;
				items.get(j).y = 2000;
			}
		}
	}
	
	public void mouseReleased()
	{
		//if (mouseOver() && justPressed)
		//{
		//	active = !active;
		//}
		if(GLOBAL.getItemsSize()>dragDownSize)
		selectBar.mouseReleased();
		//keepPressing = false;
		//for(ArtistButton c: items)
		//{
		//	if(c.mouseOver())
		//		c.mouseReleased();
		//}
		if(onItem && onItemIndex>=0)
		{
			
//Commented after v78/////////////////////////////////////////////////////////////////////////////
			GLOBAL.testArtist = items.get(onItemIndex).artist;
			
			ReleaseMenu.artistStatusChanged = true;
			
			GLOBAL.db.getArtistInfo(GLOBAL.testArtist);
			GLOBAL.testGPie.updateNumber(GLOBAL.testArtist.recordsCountBySex[1],GLOBAL.testArtist.recordsCountBySex[0]);
			ArrayList<ChartElement>newAgeData = new ArrayList<ChartElement>();
			for(int i = 0; i < GLOBAL.testArtist.recordsCountByAge.length; i++) {
				newAgeData.add(new ChartElement(GLOBAL.testArtist.recordsCountByAge[i], i*10+""));
			}
			GLOBAL.testBar.setData(newAgeData);

			ArrayList<ChartElement>newCountryData = new ArrayList<ChartElement>();
			
			Iterator<Entry<String, Long>> it = GLOBAL.testArtist.recordsCountByCountries.entrySet().iterator();
			while (it.hasNext()) {
			     Map.Entry<String, Long> pairs = (Map.Entry<String, Long>) it.next();
//			     System.out.println(pairs.getKey() + " = " + pairs.getValue());
				 newCountryData.add(new ChartElement(pairs.getKey(), pairs.getValue()));

			}
			GLOBAL.testPie.setData(newCountryData);
			
			ArtistLFM lfmInfo = GLOBAL.http.getLFMArtistInfo(GLOBAL.testArtist.mbid);
			//for some reason we are crashing here so i will add try and catch
			try {
				if (lfmInfo != null && lfmInfo.image_large != "") {
					GLOBAL.testImage = GLOBAL.processing
							.loadImage(lfmInfo.image_large);
				} else {
					GLOBAL.testImage = GLOBAL.processing
							.loadImage(GLOBAL.baseDir + "noImage.jpg");
				}
			} catch (Exception e) {
				GLOBAL.testImage = GLOBAL.processing
						.loadImage(GLOBAL.baseDir + "noImage.jpg");
			}
//			ArtistMB mbInfo = GLOBAL.http.getMBAristInfo(mbid);
			//GLOBAL.testImage
//			GLOBAL.testArtist.LFMInfo = GLOBAL.http.getLFMArtistInfo(GLOBAL.testArtist.mbid);
//			GLOBAL.testArtist.MBInfo = GLOBAL.http.getMBAristInfo(GLOBAL.testArtist.mbid);
//End of comment after v78/////////////////////////////////////////////////////////////////////////////
			
			items.get(onItemIndex).mouseReleased();
			GLOBAL.relationTree.updateCoreArtistId(GLOBAL.testArtist.id);
			GLOBAL.relationTree.redraw();


		}
		
		justPressed = false;
		onUpButton = onDownButton = false;
	}
	public void mousePressed()
	{
		justPressed = true;
		
		if(onItem && onItemIndex>=0)
			items.get(onItemIndex).mousePressed();
		else if(upButton.mouseOver())
			onUpButton = true;
		else if(downButton.mouseOver())
			onDownButton = true;
	}
	
	public void mouseDragged()
	{
		if(onItem && onItemIndex>=0 )//&& items.get(onItemIndex).active == false)
			items.get(onItemIndex).dragged = true;//.mouseDragged();
	}
	
	@Override
	public boolean mouseOver()
	{
		float val = 0;
		// Find from what ArtistButton we have to print, based on the value of the scrollbar in this moment
		if(GLOBAL.getItemsSize()>dragDownSize)
			val = PApplet.map(selectBar.value,0, 1-selectBar.size ,0,GLOBAL.getItemsSize() - dragDownSize +1);
		else 
			val = 0;
		
		//PApplet.println((GLOBAL.DEBUGCOUNTER++)+"val= "+val);
				
		if(GLOBAL.processing.mouseX >= x && GLOBAL.processing.mouseX <= x + width && 
    			GLOBAL.processing.mouseY >= y && GLOBAL.processing.mouseY <= y + height) return true;
		else
		{
			for(int j= (int)val; j < (int)val + dragDownSize && j < GLOBAL.getItemsSize(); j++) 
			{
				if(items.get(j).mouseOver())
				{
					onItem = true;
					onItemIndex = j;
					return true;
				}
			}
		}
		onItem = false;
		onItemIndex = -1;
    	return false;
		
	}

}